package com.uoko.common.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 * url 转码和解码
 * </p>
 *
 * @author shaohua
 * Email xiaomabenten@gamil.com
 * created by 2018/6/7
 */


public class UrlUtil {
    private static final Logger logger = LoggerFactory.getLogger(UrlUtil.class);

    private final static String ENCODE = "GBK";

    /**
     * URL 解码
     *
     * @param str
     * @return
     */
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            logger.error("URL解码失败 ex={}", e.getMessage(), e);
        }
        return result;
    }

    /**
     * URL 转码
     *
     * @param str
     * @return
     */
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            logger.error("URL转码失败 ex={}", e.getMessage(), e);
        }
        return result;
    }
}
